package com.kingdee.eas.crm.market.app;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.IObjectPK;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.entity.SelectorItemCollection;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSUuid;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.crm.market.MarketPlanCollection;
import com.kingdee.eas.crm.market.MarketPlanEntryInfo;
import com.kingdee.eas.crm.market.MarketPlanInfo;
import com.kingdee.eas.crm.market.ThemeEnum;
import com.kingdee.eas.fdc.basecrm.CRMHelper;
import com.kingdee.eas.fdc.basedata.FDCBillInfo;
import com.kingdee.eas.fdc.basedata.FDCBillStateEnum;
import com.kingdee.eas.fdc.basedata.FDCCommonServerHelper;
import com.kingdee.eas.fdc.market.EnterpriseSchemeFactory;
import com.kingdee.eas.util.app.ContextUtil;
import com.kingdee.util.NumericExceptionSubItem;

public class MarketPlanControllerBean extends AbstractMarketPlanControllerBean
{
    private static Logger logger =
        Logger.getLogger("com.kingdee.eas.crm.market.app.MarketPlanControllerBean");
    protected void checkNameDup(Context context, FDCBillInfo fdcbillinfo)
    throws BOSException, EASBizException
{
}

	protected void _audit(Context ctx, BOSUuid billId)
	    throws BOSException, EASBizException
	{
	/*  53*/        MarketPlanInfo billInfo = (MarketPlanInfo)getValue(ctx, new ObjectUuidPK(billId));
	/*  55*/        billInfo.setId(billId);
	/*  56*/        billInfo.setState(FDCBillStateEnum.AUDITTED);
	/*  57*/        billInfo.setUseing(true);
	/*  58*/        billInfo.setAuditor(ContextUtil.getCurrentUserInfo(ctx));
	/*  59*/        billInfo.setAuditTime(FDCCommonServerHelper.getServerTime());
	/*  61*/        SelectorItemCollection selector = new SelectorItemCollection();
	/*  62*/        selector.add("state");
	/*  63*/        selector.add("auditor");
	/*  64*/        selector.add("auditTime");
	/*  65*/        selector.add("useing");
	/*  67*/        _updatePartial(ctx, billInfo, selector);
	/* 129*/        EntityViewInfo evInfo = new EntityViewInfo();
	/* 130*/        FilterInfo filter = new FilterInfo();
	/* 131*/        filter.getFilterItems().add(new FilterItemInfo("id", billId, CompareType.NOTEQUALS));
	/* 132*/        filter.getFilterItems().add(new FilterItemInfo("sellProject.id", billInfo.getSellProject().getId().toString()));
	/* 133*/        filter.getFilterItems().add(new FilterItemInfo("useing", Boolean.TRUE));
	/* 135*/        evInfo.setFilter(filter);
	/* 136*/        MarketPlanCollection coll = getMarketPlanCollection(ctx, evInfo);
	/* 137*/        SelectorItemCollection updateselector = new SelectorItemCollection();
	/* 138*/        updateselector.add("useing");
	/* 139*/        for(int i = 0; i < coll.size(); i++)
	    {
	/* 140*/            MarketPlanInfo updateInfo = coll.get(0);
	/* 141*/            updateInfo.setUseing(false);
	/* 142*/            _updatePartial(ctx, updateInfo, updateselector);
	    }
	
	}
	
	protected void _unAudit(Context ctx, BOSUuid billId)
	    throws BOSException, EASBizException
	{
	/* 147*/        MarketPlanInfo billInfo = (MarketPlanInfo)getValue(ctx, new ObjectUuidPK(billId));
	/* 149*/        boolean isUpdate = false;
	/* 150*/        if(billInfo.isUseing())
	/* 151*/            isUpdate = true;
	/* 154*/        billInfo.setId(billId);
	/* 155*/        billInfo.setState(FDCBillStateEnum.SUBMITTED);
	/* 156*/        billInfo.setAuditor(null);
	/* 157*/        billInfo.setAuditTime(null);
	/* 158*/        billInfo.setUseing(false);
	/* 159*/        SelectorItemCollection selector = new SelectorItemCollection();
	/* 160*/        selector.add("state");
	/* 161*/        selector.add("auditor");
	/* 162*/        selector.add("auditTime");
	/* 163*/        selector.add("useing");
	/* 165*/        _updatePartial(ctx, billInfo, selector);
	/* 167*/        if(isUpdate)
	    {
	/* 168*/            EntityViewInfo viewInfo = new EntityViewInfo();
	/* 169*/            FilterInfo filter = new FilterInfo();
	/* 170*/            filter.getFilterItems().add(new FilterItemInfo("sellProject.id", billInfo.getSellProject().getId()));
	/* 171*/            filter.getFilterItems().add(new FilterItemInfo("state", "4AUDITTED"));
	/* 172*/            filter.getFilterItems().add(new FilterItemInfo("id", billId, CompareType.NOTEQUALS));
	/* 173*/            viewInfo.setFilter(filter);
	/* 175*/            MarketPlanCollection coll = getMarketPlanCollection(ctx, viewInfo);
	/* 176*/            CRMHelper.sortCollection(coll, new String[] {
	/* 176*/                "planYear", "planMonth", "version"
	        }, false);
	/* 178*/            if(coll.size() > 0)
	        {
	/* 179*/                SelectorItemCollection updateselector = new SelectorItemCollection();
	/* 180*/                updateselector.add("useing");
	/* 181*/                MarketPlanInfo updateInfo = coll.get(0);
	/* 182*/                updateInfo.setUseing(true);
	/* 183*/                _updatePartial(ctx, updateInfo, updateselector);
	        }
	    }
	}
	
	protected void _submit(Context ctx, IObjectPK pk, IObjectValue model)
	    throws BOSException, EASBizException
	{
	/* 189*/        MarketPlanInfo info = (MarketPlanInfo)model;
	/* 190*/        for(int i = 0; i < info.getEntry().size(); i++)
	    {
	/* 191*/            MarketPlanEntryInfo entry = info.getEntry().get(i);
	/* 192*/            if(entry.getState().equals(ThemeEnum.Canceled))
	        {
	/* 193*/                for(int j = 0; j < entry.getSellPlanEntry().size(); j++)
	/* 194*/                    entry.getSellPlanEntry().get(j).setState(ThemeEnum.Canceled);
	
	        }
	    }
	
	/* 198*/        super._submit(ctx, pk, model);
	}
	
	protected IObjectPK _submit(Context ctx, IObjectValue model)
	    throws BOSException, EASBizException
	{
	/* 202*/        MarketPlanInfo info = (MarketPlanInfo)model;
	/* 203*/        for(int i = 0; i < info.getEntry().size(); i++)
	    {
	/* 204*/            MarketPlanEntryInfo entry = info.getEntry().get(i);
	/* 205*/            if(entry.getState().equals(ThemeEnum.Canceled))
	        {
	/* 206*/                for(int j = 0; j < entry.getSellPlanEntry().size(); j++)
	/* 207*/                    entry.getSellPlanEntry().get(j).setState(ThemeEnum.Canceled);
	
	        }
	    }
	
	/* 211*/        return super._submit(ctx, model);
	}
	
	protected void checkDelete(Context ctx, IObjectPK pk)
	    throws EASBizException, BOSException
	{
	/* 215*/        FilterInfo filter = new FilterInfo();
	/* 216*/        filter.getFilterItems().add(new FilterItemInfo("enterprisePlan.id", pk.toString()));
	/* 217*/        if(EnterpriseSchemeFactory.getLocalInstance(ctx).exists(filter))
	/* 218*/            throw new EASBizException(new NumericExceptionSubItem("100", "\u5DF2\u5173\u8054\u4F01\u5212\u5B9E\u65BD\uFF0C\u7981\u6B62\u5220\u9664\uFF01"));
	/* 220*/        else
	/* 220*/            return;
	}
	
	protected void _delete(Context ctx, IObjectPK pk)
	    throws BOSException, EASBizException
	{
	/* 222*/        checkDelete(ctx, pk);
	/* 223*/        super._delete(ctx, pk);
	}
	
	protected void _delete(Context ctx, IObjectPK arrayPK[])
	    throws BOSException, EASBizException
	{
	/* 227*/        for(int i = 0; i < arrayPK.length; i++)
	/* 228*/            checkDelete(ctx, arrayPK[i]);
	
	/* 230*/        super._delete(ctx, arrayPK);
	}
}